package org.lease.web.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.lease.common.result.Result;
import org.lease.model.dto.AttrDTO;
import org.lease.model.entity.AttrKey;
import org.lease.model.entity.AttrValue;
import org.lease.service.AttrKeyService;
import org.lease.service.AttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "房间基本信息管理")
@RestController
@RequestMapping("/admin/attr")
public class AttrController {
    @Autowired
    private AttrKeyService attrKeyService;
    @Autowired
    private AttrValueService attrValueService;

    @Operation(summary = "查询所有房间基本信息")
    @GetMapping("/list")
    public Result<List<AttrDTO>> list() {
        List<AttrDTO> list = attrKeyService.listWithValue();

        log.info("查询所有房间基本信息，结果：{}", list);
        return Result.success(list);
    }

    @Operation(summary = "保存或更新房间属性名")
    @PostMapping("/key/saveOrUpdate")
    public Result saveOrUpdateKey(@RequestBody AttrKey attrKey) {
        attrKeyService.saveOrUpdate(attrKey);
        return Result.success();
    }

    @Operation(summary = "根据 Key ID 删除房间属性")
    @DeleteMapping("/key/deleteById")
    public Result removeKeyById(Long attrKeyId) {
        attrKeyService.removeById(attrKeyId);
        attrValueService.removeByAttrKeyId(attrKeyId);
        return Result.success();
    }

    @Operation(summary = "保存或更新房间属性值")
    @PostMapping("/value/saveOrUpdate")
    public Result saveOrUpdateValue(@RequestBody AttrValue attrValue) {
        attrValueService.saveOrUpdate(attrValue);
        return Result.success();
    }

    @Operation(summary = "根据 Key ID 删除房间属性")
    @DeleteMapping("/value/deleteById")
    public Result removeValueById(Long id) {
        attrValueService.removeById(id);
        return Result.success();
    }
}
